<template>
  <header class="header" :class="{ 'header-scrolled': isScrolled }">
    <nav class="nav">
      <div class="logo">
        <Logo :size="40" :animated="true" show-text />
      </div>
      <ul class="nav-menu">
        <li v-for="item in navItems" :key="item.id">
          <a
            :href="`#${item.id}`"
            class="nav-link"
            :class="{ 'nav-link-active': activeSection === item.id }"
            @click="scrollToSection(item.id)"
          >
            {{ item.label }}
          </a>
        </li>
      </ul>
      <div class="nav-toggle" @click="toggleMenu">
        <span class="nav-toggle-line" :class="{ 'nav-toggle-line-active': isMenuOpen }"></span>
        <span class="nav-toggle-line" :class="{ 'nav-toggle-line-active': isMenuOpen }"></span>
        <span class="nav-toggle-line" :class="{ 'nav-toggle-line-active': isMenuOpen }"></span>
      </div>
    </nav>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import Logo from './Logo.vue'

interface NavItem {
  id: string
  label: string
}

const props = defineProps<{
  activeSection: string
}>()

const emit = defineEmits<{
  sectionClick: [sectionId: string]
}>()

const navItems: NavItem[] = [
  { id: 'home', label: '首页' },
  { id: 'about', label: '关于我' },
  { id: 'projects', label: '项目' },
  { id: 'contact', label: '联系' }
]

const isScrolled = ref(false)
const isMenuOpen = ref(false)

const handleScroll = () => {
  isScrolled.value = window.scrollY > 50
}

const scrollToSection = (sectionId: string) => {
  const element = document.getElementById(sectionId)
  if (element) {
    element.scrollIntoView({ behavior: 'smooth' })
  }
  emit('sectionClick', sectionId)
  isMenuOpen.value = false
}

const toggleMenu = () => {
  isMenuOpen.value = !isMenuOpen.value
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
.header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.header-scrolled {
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.logo-text {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
}

.logo-gradient {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.nav-menu {
  display: flex;
  list-style: none;
  gap: 2rem;
  margin: 0;
  padding: 0;
}

.nav-link {
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 500;
  padding: 0.5rem 1rem;
  border-radius: 25px;
  transition: all 0.3s ease;
  position: relative;
}

.nav-link:hover {
  color: var(--primary-color);
  background: rgba(102, 126, 234, 0.1);
}

.nav-link-active {
  color: var(--primary-color);
  background: rgba(102, 126, 234, 0.15);
}

.nav-link-active::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  background: var(--primary-color);
  border-radius: 1px;
}

.nav-toggle {
  display: none;
  flex-direction: column;
  cursor: pointer;
  padding: 0.5rem;
}

.nav-toggle-line {
  width: 25px;
  height: 3px;
  background: var(--text-primary);
  margin: 3px 0;
  transition: 0.3s;
  border-radius: 2px;
}

.nav-toggle-line-active:nth-child(1) {
  transform: rotate(-45deg) translate(-5px, 6px);
}

.nav-toggle-line-active:nth-child(2) {
  opacity: 0;
}

.nav-toggle-line-active:nth-child(3) {
  transform: rotate(45deg) translate(-5px, -6px);
}

@media (max-width: 768px) {
  .nav {
    padding: 1rem;
  }

  .nav-menu {
    position: fixed;
    left: -100%;
    top: 70px;
    flex-direction: column;
    background-color: white;
    width: 100%;
    text-align: center;
    transition: 0.3s;
    box-shadow: 0 10px 27px rgba(0, 0, 0, 0.05);
    padding: 2rem 0;
  }

  .nav-toggle {
    display: flex;
  }
}
</style>